package org.adream.account.service;

import org.adream.account.dao.DomainProfileDao;
import org.adream.account.entity.DomainProfileEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.rest.api.model.SiteDomainResp;
import org.adream.account.rest.api.model.SiteInfoResp;
import org.adream.account.rest.api.model.SiteResp;
import org.adream.account.util.IdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 多站点
 * @author: magic chen
 * @date: 2022/8/3 16:11
 **/
@Service
public class DomainProfileService {

    private final static Logger logger = LoggerFactory.getLogger(DomainProfileService.class);

    @Autowired
    DomainProfileDao domainProfileDao;

    @Autowired
    private HttpSession session;


    // 新增站点
    public ResultModel<String> addSite(String siteName) {
        if (domainProfileDao.checkSiteNameExist(siteName) != null) {
            return new ResultModel<>(ResultModel.ERR_DATABASE, ResultModel.HAS_FOUND_OBJECT, "该站点已存在");
        }
        DomainProfileEntity site = new DomainProfileEntity();
        site.setId(IdGenerator.getGuid());
        site.setDataType("P");
        site.setParentId("0");
        site.setDataName("站点名");
        site.setDataKey("site");
        site.setDataValue(siteName);
        site.setDr(1);
        site.setCreator((String) session.getAttribute("uid"));
        site.setModifier((String) session.getAttribute("uid"));
        int row = domainProfileDao.insertSelective(site);
        if (row > 0) {
            return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
        }
        return new ResultModel<>(ResultModel.ERR_DATABASE, ResultModel.ERR_DATABASE_MSG, null);
    }

    // 列出所有站点
    public ResultModel<List<SiteResp>> listAllSite() {
        List<DomainProfileEntity> data = domainProfileDao.getAvailableSite();
        if (data.isEmpty()) {
            return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, new ArrayList<>());
        }
        List<SiteResp> resp = data.stream().map(src -> {
            SiteResp r = new SiteResp();
            r.setId(src.getId());
            r.setSiteName(src.getDataValue());
            r.setCts(src.getCts());
            return r;
        }).collect(Collectors.toList());
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, resp);
    }

    // 反显站点信息
    public ResultModel<SiteResp> viewSiteById(String siteId) {
        DomainProfileEntity data = domainProfileDao.getById(siteId);
        if (data == null) {
            return new ResultModel<>(ResultModel.NOT_FOUND_OBJECT, ResultModel.NOT_FOUND_OBJECT_MSG, null);
        }
        SiteResp resp = new SiteResp();
        resp.setId(data.getId());
        resp.setCts(data.getCts());
        resp.setSiteName(data.getDataValue());
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, resp);
    }

    // 编辑站点信息 其实也就只给编辑站点名
    public ResultModel<String> updateSiteName(String siteId, String siteName) {
        if (domainProfileDao.checkSiteNameExist(siteName) != null) {
            return new ResultModel<>(ResultModel.ERR_DATABASE, ResultModel.HAS_FOUND_OBJECT, "该站点名称已被使用");
        }
        DomainProfileEntity update = new DomainProfileEntity();
        update.setId(siteId);
        update.setDr(1);
        update.setDataValue(siteName);
        update.setModifier((String) session.getAttribute("uid"));
        domainProfileDao.updateSelective(update);
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
    }

    // 删除站点
    public ResultModel<String> deleteSite(String siteId) {
        domainProfileDao.deleteById(siteId);
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
    }

    // 列出某一站点的 所有域名
    public ResultModel<List<SiteDomainResp>> listAllDomainBySite(String siteId) {
        List<DomainProfileEntity> data = domainProfileDao.getAvailableInfoByParentId(siteId);
        if (data.isEmpty()) {
            return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, new ArrayList<>());
        }
        List<SiteDomainResp> resp = data.stream()
                .filter(s -> s.getDataKey().equals("domain"))
                .map(src -> {
                    SiteDomainResp r = new SiteDomainResp();
                    r.setId(src.getId());
                    r.setDomain(src.getDataValue());
                    r.setCts(src.getCts());
                    return r;
                }).collect(Collectors.toList());
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, resp);
    }

    // 给某站点新增域名 （key为domain 可重复; domainValue 检查重复 ）
    public ResultModel<String> addDomainForSite(String siteId, String domainName) {
        if (domainProfileDao.checkDomainNameExist(siteId, domainName) != null) {
            return new ResultModel<>(ResultModel.ERR_DATABASE, ResultModel.HAS_FOUND_OBJECT, "该站点已配置相同域名");
        }
        DomainProfileEntity domain = new DomainProfileEntity();
        domain.setId(IdGenerator.getGuid());
        domain.setDataType("C");
        domain.setParentId(siteId);
        domain.setDataName("域名");
        domain.setDataKey("domain");
        domain.setDataValue(domainName);
        domain.setDr(1);
        domain.setCreator((String) session.getAttribute("uid"));
        domain.setModifier((String) session.getAttribute("uid"));
        int row = domainProfileDao.insertSelective(domain);
        if (row > 0) {
            return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
        }
        return new ResultModel<>(ResultModel.ERR_DATABASE, ResultModel.ERR_DATABASE_MSG, null);
    }

    // 反显某一域名
    public ResultModel<SiteDomainResp> viewDomainById(String id) {
        DomainProfileEntity data = domainProfileDao.getById(id);
        if (data == null) {
            return new ResultModel<>(ResultModel.NOT_FOUND_OBJECT, ResultModel.NOT_FOUND_OBJECT_MSG, null);
        }
        SiteDomainResp resp = new SiteDomainResp();
        resp.setId(data.getId());
        resp.setDomain(data.getDataValue());
        resp.setCts(data.getCts());
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, resp);
    }

    // 编辑某一域名
    public ResultModel<String> updateDomainName(String domainId, String domainName) {
        DomainProfileEntity data = domainProfileDao.getById(domainId);
        if (domainProfileDao.checkDomainNameExist(data.getParentId(), domainName) != null) {
            return new ResultModel<>(ResultModel.ERR_DATABASE, ResultModel.HAS_FOUND_OBJECT, "该站点已配置相同域名");
        }
        DomainProfileEntity update = new DomainProfileEntity();
        update.setId(domainId);
        update.setDr(1);
        update.setDataValue(domainName);
        update.setModifier((String) session.getAttribute("uid"));
        domainProfileDao.updateSelective(update);
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
    }

    // 删除某一域名
    public ResultModel<String> deleteDomain(String domainId) {
        domainProfileDao.deleteById(domainId);
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
    }

    // 列出某一站点的所有数据
    public ResultModel<List<SiteInfoResp>> listAllSiteInfo(String siteId) {
        List<DomainProfileEntity> data = domainProfileDao.getAvailableInfoByParentId(siteId);
        if (data.isEmpty()) {
            return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, new ArrayList<>());
        }
        List<SiteInfoResp> resp = data.stream()
                .filter(s -> !s.getDataKey().equals("domain"))
                .map(src -> {
                    SiteInfoResp r = new SiteInfoResp();
                    r.setId(src.getId());
                    r.setDataName(src.getDataName());
                    r.setDataKey(src.getDataKey());
                    r.setDataValue(src.getDataValue());
                    return r;
                }).collect(Collectors.toList());
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, resp);
    }

    // 给某站点新增数据  （数据key 检查是否重复）
    public ResultModel<String> addInfoForSite(String siteId, String dataName, String dataKey, String dataValue) {
        if (domainProfileDao.checkSiteInfoKeyExist(siteId, dataKey) != null) {
            return new ResultModel<>(ResultModel.ERR_DATABASE, ResultModel.HAS_FOUND_OBJECT, "该站点已配置此key");
        }
        DomainProfileEntity info = new DomainProfileEntity();
        info.setId(IdGenerator.getGuid());
        info.setDataType("C");
        info.setParentId(siteId);
        info.setDataName(dataName);
        info.setDataKey(dataKey);
        info.setDataValue(dataValue);
        info.setDr(1);
        info.setCreator((String) session.getAttribute("uid"));
        info.setModifier((String) session.getAttribute("uid"));
        int row = domainProfileDao.insertSelective(info);
        if (row > 0) {
            return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
        }
        return new ResultModel<>(ResultModel.ERR_DATABASE, ResultModel.ERR_DATABASE_MSG, null);
    }

    // 反显 某一数据
    public ResultModel<SiteInfoResp> viewSiteInfoById(String infoId) {
        DomainProfileEntity data = domainProfileDao.getById(infoId);
        if (data == null) {
            return new ResultModel<>(ResultModel.NOT_FOUND_OBJECT, ResultModel.NOT_FOUND_OBJECT_MSG, null);
        }
        SiteInfoResp resp = new SiteInfoResp();
        resp.setId(data.getId());
        resp.setDataName(data.getDataName());
        resp.setDataKey(data.getDataKey());
        resp.setDataValue(data.getDataValue());
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, resp);
    }

    // 编辑 某一数据
    public ResultModel<String> updateSiteInfo(String infoId, String dataName, String dataKey, String dataValue) {
        DomainProfileEntity update = new DomainProfileEntity();
        update.setId(infoId);
        update.setDr(1);
        update.setDataName(dataName);
        update.setDataKey(dataKey);
        update.setDataValue(dataValue);
        domainProfileDao.updateSelective(update);
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
    }

    // 删除 某一数据
    public ResultModel<String> deleteInfo(String infoId) {
        domainProfileDao.deleteById(infoId);
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
    }

    // 根据访问的域名 获取当前站点的相关信息
    public ResultModel<Map<String, String>> getInfoByDomain(String domain) {
        DomainProfileEntity row = domainProfileDao.getByDomainName(domain);
        if (row == null) {
            // 访问的域名匹配不到站点
            return new ResultModel<>(ResultModel.NOT_FOUND_OBJECT, ResultModel.NOT_FOUND_OBJECT_MSG, null);
        }
        List<DomainProfileEntity> originData = domainProfileDao.getAvailableInfoByParentId(row.getParentId());
        Map<String, String> result = originData.stream()
                .filter(s -> !s.getDataKey().equals("domain"))
                .collect(Collectors.toMap(DomainProfileEntity::getDataKey, DomainProfileEntity::getDataValue));
        return new ResultModel<>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, result);
    }


}
